package demo1;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Author liangzai
 * @Description:
 */
public class Training {
    //增减字符串匹配
    public int[] diStringMatch(String s) {
        int n = s.length();
        int left = 0;
        int right = n;
        int[] ret = new int[n+1];
        for (int i = 0; i < n; i++) {
            char ch = s.charAt(i);
            if(ch=='I') ret[i] = left++;
            else ret[i] = right--;
        }
        ret[n] = left;
        return ret;
    }

    //最长回文串
    public int longestPalindrome(String s) {
        HashMap<Character,Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            map.put(ch,map.getOrDefault(ch,0)+1);
        }
        int ret = 0;
        Set set = map.entrySet();
        for(Map.Entry<Character,Integer> x:map.entrySet()){
            ret += x.getValue()/2*2;
        }
        return ret<s.length()?ret+1:ret;
    }
}















